package phone.qyg.com.qyg.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.app.adapter.recycler.CommonAdapter;
import com.app.adapter.recycler.MultiItemTypeAdapter;
import com.app.adapter.recycler.RecyclerSpace;
import com.app.adapter.recycler.base.ViewHolder;
import com.app.application.QXApplication;
import com.app.utils.CommonUtil;
import com.app.utils.SystemPrintl;

import java.util.List;

import phone.qyg.com.qyg.R;
import phone.qyg.com.qyg.model.Base;

/**
 * 作者：lblbh on 2017/3/21 18:23
 * 邮箱：lanbuhan@163.com
 * 功能：TODO
 */
public class PopuRecycle {

    public static PopupWindow popupWindow;
    public static enum EnumType {
        THMEE,  //场馆主题
        TIME,   //日程安排
        LEVL    //活动级别
    }

    public static PopupWindow setPupoClassifyCourse(Activity activity, View v, List<Base> models, final Base themeModel, final OnClickState onClickState){

        LayoutInflater inflater = (LayoutInflater) QXApplication.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.popuwind, null);

        final RecyclerView recycler = (RecyclerView) view.findViewById(R.id.recycler);
        final TextView text = (TextView) view.findViewById(R.id.text);

        popupWindow = new PopupWindow(view,
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        if (android.os.Build.VERSION.SDK_INT >=24) {
            int[] a = new int[2];
            v.getLocationInWindow(a);
            popupWindow.showAtLocation(activity.getWindow().getDecorView(), Gravity.NO_GRAVITY, a[0] , a[1]+v.getHeight());
//            popupWindow.showAtLocation((QXApplicat.getWindow().getDecorView(), Gravity.NO_GRAVITY, 0 , a[1]+v.getHeight());
        } else{
            popupWindow.showAsDropDown(v);
        }
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
//		popupWindow.update();
        popupWindow.setBackgroundDrawable(new BitmapDrawable());

        /**
         * 设置布局控制器
         */
        GridLayoutManager girdLayoutManager = new GridLayoutManager(QXApplication.getContext(),4, GridLayoutManager.VERTICAL,false);
        RecyclerSpace space = new RecyclerSpace(4, Color.WHITE);
        recycler.addItemDecoration(space);
        recycler.setLayoutManager(girdLayoutManager);


        setRecycleDatas(popupWindow,recycler,models, EnumType.THMEE,themeModel,onClickState);


        final PopupWindow finalPopupWindow = popupWindow;
        text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finalPopupWindow.dismiss();
            }
        });

        return popupWindow;
    }


    /**
     * 直接设置集合
     * @param v
     * @param models
     * @param themeModel
     * @param onClickState
     * @return
     */
    public static PopupWindow setPopuThemes(Context context,View v,List<Base> models,final Base themeModel, final OnClickState onClickState){
        LayoutInflater inflater = (LayoutInflater) QXApplication.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.popuwind, null);

        final RecyclerView recycler = (RecyclerView) view.findViewById(R.id.recycler);
        final TextView text = (TextView) view.findViewById(R.id.text);
        final LinearLayout empty_layout = (LinearLayout) view.findViewById(R.id.empty_layout);

        popupWindow = new PopupWindow(view,
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

//        popupWindow.showAsDropDown(v);
        SystemPrintl.systemPrintl("我当前的版本号是--->>>"+android.os.Build.VERSION.SDK_INT);
        if (android.os.Build.VERSION.SDK_INT >=24) {
            int[] a = new int[2];
            v.getLocationInWindow(a);
//            v.getLocationOnScreen(a);
            SystemPrintl.systemPrintl("我当前的版本号是--高度->>>"+a[1]+",控件本身高度:"+v.getHeight());
//            popupWindow.showAtLocation(((Activity) context).getWindow().getDecorView(), Gravity.NO_GRAVITY, 0 , a[1]+v.getHeight());
            int height =  a[1]+v.getHeight();

//            popupWindow.showAtLocation(((Activity) context).getWindow().getDecorView(), Gravity.START| Gravity.TOP , 0, a[1]+v.getHeight());
            if(height >0){
                Rect rectangle= new Rect();
                ((Activity) context).getWindow().getDecorView().getWindowVisibleDisplayFrame(rectangle);
                int top = rectangle.top;
                SystemPrintl.systemPrintl("状态栏的高度--->>"+top);
                ViewGroup.LayoutParams params = empty_layout.getLayoutParams();
                params.height = (height - top);
                empty_layout.setLayoutParams(params);
            }
            popupWindow.showAsDropDown(v);
        } else{
            popupWindow.showAsDropDown(v);
        }
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
//		popupWindow.update();
        popupWindow.setBackgroundDrawable(new BitmapDrawable());

        GridLayoutManager girdLayoutManager = new GridLayoutManager(QXApplication.getContext(),4, GridLayoutManager.VERTICAL,false);
        RecyclerSpace space = new RecyclerSpace(4, Color.WHITE);
        recycler.addItemDecoration(space);
        recycler.setLayoutManager(girdLayoutManager);

        setRecycleDatas(popupWindow,recycler,models, EnumType.THMEE,themeModel,onClickState);

        final PopupWindow finalPopupWindow = popupWindow;
        text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finalPopupWindow.dismiss();
            }
        });
        empty_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finalPopupWindow.dismiss();
            }
        });

        return popupWindow;
    }


    /**
     * 设置数据源
     * @param models
     */
    private static void setRecycleDatas(final PopupWindow popupWindow, RecyclerView recycler, List models, final EnumType type, final Base themeModel , final OnClickState onClickState){
        if(!CommonUtil.isRequestList(models))
            return;
        final CommonAdapter adapter = new CommonAdapter<Object >(QXApplication.getContext(),R.layout.item_city,models) {
            @Override
            protected void convert(ViewHolder holder, Object model, int position) {
                String title = "";
                if(type.equals(EnumType.THMEE)){
                    Base base = (Base) model;
                    title = base.title;
                    if(themeModel != null && base.title.equals(themeModel.title)){
                        holder.setSelect(R.id.item_city,true);
                        holder.setTextColorRes(R.id.item_city,R.color.login_fill_btn);
                    }
                }

                holder.setText(R.id.item_city,title);
            }
        };

        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                if(popupWindow != null && popupWindow.isShowing()){
                    popupWindow.dismiss();
                }
//                SystemPrintl.systemPrintl("点击---->"+position);
                Object obj = adapter.getData().get(position);
                onClickState.OnItemClick(obj,type);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        recycler.setAdapter(adapter);
    }


    public interface OnClickState{
        public void OnItemClick(Object model, EnumType enumType);
    }
}
